import gulp from 'gulp';
import concat from 'gulp-concat';
import less from 'gulp-less';

const MOOD_UI = {
    MAIN_NAME: 'ui',
    MAIN_NAME_WATCH: 'watch-ui',
    MAIN_SRC: './src/ui/**/**/*.less',
    MAIN_DEST: './src/ui/styles/dist',
}

const MOOD_BlOG = {
    // 主项目
    MAIN_NAME: 'main',
    MAIN_NAME_WATCH: 'watch-main',
    MAIN_SRC: './src/assets/styles/**/*.less',
    MAIN_DEST: './src/assets/styles/dist',
}

startLessTask(MOOD_UI.MAIN_NAME, MOOD_UI.MAIN_SRC, MOOD_UI.MAIN_DEST);

startLessTask(MOOD_BlOG.MAIN_NAME, MOOD_BlOG.MAIN_SRC, MOOD_BlOG.MAIN_DEST);

watchParallel([MOOD_UI.MAIN_NAME_WATCH, MOOD_BlOG.MAIN_NAME_WATCH]);

function startLessTask(taskName, src, dest, opts = { isWatch: true }) {
    gulp.task(taskName, function () {
        return gulp.src(src)
            .pipe(less())
            .pipe(concat('index.css'))
            .pipe(gulp.dest(dest));
    });
    if (opts.isWatch) {
        startWatchTask(taskName, src);
    }
}
function startWatchTask(taskName, src, opts = { recursive: true }) {
    gulp.task(`watch-${taskName}`, function () {
        gulp.watch(src, opts, gulp.series(taskName));
    });
}
function watchParallel(tasks = []) {
    gulp.task('watch', gulp.parallel(...tasks));
}